﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.Models;
using Website.Models.ModelMetadata;

namespace Website.Controllers
{
    public class HomeController : Controller
    {
        private KFashionEntities db = new KFashionEntities();
        private int itemPerPage = 10;

        public ActionResult Contact()
        {
            return View(db.Settings.FirstOrDefault(s => s.Name == "Contact"));
        }

        public ActionResult About()
        {
            return View(db.Settings.FirstOrDefault(s => s.Name == "About"));
        }

        public ActionResult Home()
        {
            ViewBag.Message = "Welcome to kfashion.vn";
            int[] ids = {102, 116, 120, 100, 126};
            var top5News = db.Products.Where(p => ids.Contains(p.ProductID)).ToList();
            return View(top5News);
        }

        public ActionResult Details(int productID = -1)
        {
            var product = db.Products.Include("Category").FirstOrDefault(p => p.ProductID == productID);
            if (productID == -1 || product == null)
                return RedirectToAction("Home");
            return View(product);
        }

        public ActionResult Product(int catID = -1, int page = 1, string search = null)
        {
            var products = db.Products.OrderByDescending(p => p.ProductID).ToList();
            if(!string.IsNullOrEmpty(search))
            {
                products = products.Where(p => p.ProductCode.ToString().ToLower().Contains(search.ToLower())).ToList();
                if (products.Count == 1)
                    return RedirectToAction("Details", new { productID = products[0].ProductID });
            }
            if(catID != -1)
            {
                Session.Add("catID",catID);
                var selectedCategory = db.Categories.Include("SubCategories").FirstOrDefault(c => c.CategoryID == catID);
                if(selectedCategory != null)
                {
                    if (selectedCategory.SubCategories.Count > 0)
                        products = products.Where(p => selectedCategory.SubCategories.Select(c=>c.CategoryID).Contains(p.CategoryID) || p.CategoryID == catID).ToList();
                    else
                        products = products.Where(p => p.CategoryID == catID).ToList();
                }
            }

            var pager = new Pager
                            {
                                CategoryID = catID, 
                                Search = search,
                                CurrentPage = page, 
                                TotalItem = products.Count, 
                                ItemPerPage = 10, 
                                Products = products.Skip((page - 1) * itemPerPage).Take(itemPerPage)
                            };
            ViewBag.Pager = pager;
            return View(pager);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult UpdateContact()
        {
            return View(db.Settings.First(s => s.Name == "Contact"));
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateContact(Setting setting)
        {
            if(ModelState.IsValid)
            {
                db.Settings.Attach(setting);
                db.ObjectStateManager.ChangeObjectState(setting, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Contact");
            }
            return UpdateContact(setting);
        }

        public ActionResult UpdateAbout()
        {
            return View(db.Settings.First(s => s.Name == "About"));
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateAbout(Setting setting)
        {
            if (ModelState.IsValid)
            {
                db.Settings.Attach(setting);
                db.ObjectStateManager.ChangeObjectState(setting, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("About");
            }
            return UpdateAbout(setting);
        }
    }
}
